<?php
	include("funcoes.php");
	include("conexao_banco.php");

	$ano   = $_POST['ano'];
	$setor = $_POST['setor'];
	$sec = $_POST['secretaria'];
	
	$texto = '';
	switch($setor){
		case 'saida_orcamento': $texto = 'ORCAMENTO';break;
		case 'saida_refin': $texto = 'REC. FINANCEIRO';break;
		case 'saida_sec_adm': $texto = 'SEMAD';break;
		case 'saida_controladoria': $texto = 'CONTROLADORIA';break;
		case 'saida_contador': $texto = 'CONTABILIDADE';break;
	
	}

	
	/* CONVERTA DATA NO PADRAO AMERICANO PARA INSERIR NO BANCO*/
	$data = brToUsa($_POST['data']);


//consulta sql
	$sql= '';
	if($sec == 0){
		$sql = "SELECT ".$setor.",numero_protocolo,numero_CI,credor,valor,nome_secretaria FROM `protocolo_empenho`,secretaria WHERE ano='".$ano."' AND `secretaria_idsecretaria`=`idsecretaria` AND ".$setor."='".$data."'";
	}else{
		$sql = "SELECT ".$setor.",numero_protocolo,numero_CI,credor,valor,nome_secretaria FROM `protocolo_empenho`,secretaria WHERE ano='".$ano."' AND `secretaria_idsecretaria`=`idsecretaria` AND `secretaria_idsecretaria`=".$sec." AND ".$setor."='".$data."'";
	}
	echo $sql;
	mysql_query("SET NAMES 'utf8'");
	$executa = mysql_query($sql);
	$num_rows = mysql_num_rows($executa);
	if($num_rows > 0){
		// montando a tabela
		$html =  '<html>';
		$html .= '<head>';
		$html .= '<style>';
		$html .= '@media print{.page {page-break-after:always}}body, table{font-size:8px;}.obs{width:480px;text-align:left;}table{border-collapse:collapse;}';
		$html .= '</style>';
		$html .= '</head>';
		$html .= '<body>';
		$html .= '<center style="font-size:14px;">RELA&Ccedil;&Atilde;O DE EMPENHO ENVIADO PARA '.$texto.'</center>';
		$html .= '<br/><br/>';
		$html .= "<table cellpadding='1' cellspacing='0' style='align:center;border: 1px #000 solid;'>";
		$html .= "<tr>";
		$html .= "<td align='center' style='border: 1px #000 solid;'>".$texto."</td>";
		$html .= "<td align='center' style='border: 1px #000 solid;'>PROTOCOLO</td>";
		$html .= "<td align='center' style='border: 1px #000 solid;'>CI</td>";
		$html .= "<td align='center' style='border: 1px #000 solid;'>CREDOR</td>";
		$html .= "<td align='center' style='border: 1px #000 solid;'>VALOR</td>";
		$html .= "<td align='center' style='border: 1px #000 solid;'>SOLICITANTE</td>";
		$html .= "<td align='center' style='border: 1px #000 solid;'>ASSINATURA RESPONSAVEL</td>";
		$html .= "</tr>";
		while ($rs = mysql_fetch_array($executa)){
			$html .= "<tr>";
			$html .= "<td align='center' style='height: 10px;border: 1px #000 solid;'>".usaToBr($rs[$setor]). "</td>";
			$html .= "<td align='center' style='border: 1px #000 solid;'>".$rs["numero_protocolo"]. "</td>";
			$html .= "<td align='center' style='border: 1px #000 solid;'>".$rs["numero_CI"]. "</td>";
			$html .= "<td style='border: 1px #000 solid;'>".utf8_decode($rs["credor"]). "</td>";
			$html .= "<td style='border: 1px #000 solid;'>".$rs["valor"]. "</td>";
			$html .= "<td style='border: 1px #000 solid;'>".$rs["nome_secretaria"]."</td>";
			$html .= "<td width='20%' style='border: 1px #000 solid;'></td>";
			$html .= "</tr>";
		}
		$html .= "</table>"; 
		$html .= "</body>";
		$html .= "</html>";
		
		//GERA ARQUIVO PDF
		require_once("dompdf/dompdf_config.inc.php");
		$dompdf = new DOMPDF();
		$dompdf->load_html($html);
		$dompdf->set_paper('a4', 'landscape');
		$dompdf->render();
		$dompdf->stream("Relatorio_empenho_".$_POST['data'].".pdf");
	}else{
		echo  "<html>";
		echo  "<body>";
		echo  "<center>";
		echo  "<br/><br/>";
		echo  "<span style='color:red;'>Registros n&atilde;o encontrados</span>";
		echo  "<br/><br/>";
		echo  "<br>";
		echo  "<br/><br/>";
		echo  "<a href=relat.php>VOLTAR</a></td>";
		echo  "</center>";
		echo  "</body>";
		echo  "</html>";
	}
	

?>